// 创建一个Socket实例
var socket = null;
initWebSocket();
console.log(socket);
var flag =false;


function heartbeat(){
    setTimeout("checkReadyState();heartbeat();", 30000);
}

function checkReadyState(){
    if(flag){
        console.log('心跳....'+new Date().getTime()+"__" +  socket.readyState);
        socket.send(new Date().getTime());
    }
}

var i = 0;
function reConnect(){
    flag = false;
    socket.close();
    socket = null;
    console.log("尝试重连.....重试次数:"+(++i));
    initWebSocket();
}

function initWebSocket(){
    socket = new WebSocket('ws://websocket.superboss.cc:8887/'+document.domain);
//    socket = new WebSocket('ws://127.0.0.1:8887/'+document.domain);//test
    socket.onopen = function(event) {
        flag = true;
        i = 0
        console.log("打开链接成功");
    };
    socket.onmessage = function(event) {
        console.log(event);
        document.location.href="https://login.taobao.com/member/login.jhtml?from=datacube&style=minisimple&minititle=&minipara=0,0,0&sub=true&redirect_url=http%3A%2F%2Fmofang.taobao.com%2Fs%2Flogin";
    };

    socket.onclose = function(event) {
        console.log("服务器或客户端断开连接!!!!!");
        setTimeout("reConnect()",5000);
    };
}

heartbeat();